// ===================================
// <copyright>
// This file is part of LABSMFController.
//
//    LABSMFController is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    LABSMFController is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with LABSMFController.  If not, see <http://www.gnu.org/licenses/>.
//    Copyright 2011 David Bakker
//
//</copyright>
// <author>David Bakker</author>
// <email>mailto:DavidFBakker@gmail.com</email>
// <created>Saturday, May 07, 2011</created>
// <lastedit>Saturday, May 07, 2011</lastedit>
// ===================================

using System;
using GHIElectronics.NETMF.Hardware;
using LABSMFController.LABS.Classses;
using LABSMFController.LABS.Services;

namespace LABSMFController.LABS.Devices
{
    public class BubblerDevice
    {
        
        private int _level;

        public BubblerDevice(string description, AnalogIn.Pin pin, int locationID, int levelEmpty)
        {
            Description = description;
            PIN = pin;
            LocationID = locationID;
            BubblerLocation = Location.Names[locationID];
            LevelEmpty = levelEmpty;

            Reset();
            StartGpmLevel = 0;
            Level = 0;
            AverageReading = 0;

            Header = "Location,AverageReading,LevelEmpty,Volume,LocationID,Description,Level,HighReading,LowReading,GPM,GP10Seconds,MAXBubblers";
            DeviceIn = new AnalogIn(PIN);
            DeviceIn.SetLinearScale(0, 3300);
        }


        public AnalogIn DeviceIn { get; set; }

        public int LowReading { get; private set; }
        public int HighReading { get; private set; }
        public string Header { get; set; }
        public int LocationID { get; set; }

        public int LevelEmpty { get; set; }

        public double Volume { get; set; }

        public string BubblerLocation { get; set; }
        public string Description { get; private set; }
        public AnalogIn.Pin PIN { get; set; }


        public double GPM { get; private set; }

        public double GP30Seconds { get; set; }

        private double _gp10Seconds;
        public double GP10Seconds
        {
            get { return _gp10Seconds; }
            set
            {
                _gp10Seconds = value;
                GPM = value*6;
                GP30Seconds = value*3;
            }
        }


        private long StartGpmTick { get; set; }

        private double StartGpmLevel { get; set; }

        private long _lastGpmTick;
        
        private long LastGpmTick
        {            
            set
            {
                _lastGpmTick = value;                
                var elapsedSpan = new TimeSpan(_lastGpmTick - StartGpmTick);
                if ( elapsedSpan.Seconds > 10)
                {
                    var elaspedLevel = StartGpmLevel - Volume;
                    
                   
                    if (elaspedLevel != 0)
                    {
                        GP10Seconds = (elaspedLevel/elapsedSpan.Seconds)*10;
                    } else
                    {
                        GP10Seconds = 0;
                    }

                    StartGpmTick = _lastGpmTick;
                    StartGpmLevel = Volume;
                    
                }
            }
        }

        public int AverageReading { get; set; }

        public int Level
        {
            get { return _level; }
            internal set
            {
                
                if (value < LowReading)
                    LowReading = value;

                if (value > HighReading)
                    HighReading = value;


                Volume = (Level - LevelEmpty)/(0.074529773*1000);

                if (StartGpmTick== 0 )
                    StartGpmTick = DateTime.Now.Ticks;

                if (StartGpmLevel == 0)
                    StartGpmLevel = Volume;

                LastGpmTick = DateTime.Now.Ticks;

                BeerProgram.Log("Bubbler " + Description + " logged " + value, LoggingSeverity.Debug);

                _level = value;
            }
        }

        public void Reset()
        {
            LowReading = 10000;
            HighReading = 0;
        }

        public string GetClientOutPut()
        {
            //Header = "Location,AverageReading,LevelEmpty,Volume,LocationID,Description,Level,HighReading,LowReading,GPM,GP10Seconds,MAXBubblers";
            string ret = BubblerLocation + "," + AverageReading + "," + LevelEmpty + "," + Volume + "," + LocationID +
                         "," +
                         Description + "," + Level + "," + HighReading + "," + LowReading + "," +GPM+ "," + GP10Seconds + ","+
                         BubblerServiceClass.MAXBubblers + ";";
            return ret;
        }
    }
}